<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <!--  <link rel="stylesheet" href="/css/custom.form.css">-->
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/static/css/custom.css}">
    <script th:src="@{/static/layui/layui.js}"></script>
    <script th:src="@{/static/js/jquery-3.5.1.min_2.js}"></script>
    <script th:src="@{/static/js/jqueryfileDownload.js}"></script>
    <script src="../../static/js/coreutil.js"></script>

</head>
<body>
<div class="table_div">
    <div id="searchParam">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="tableName" class="layui-input" autocomplete="off" placeholder="请输入表名">
            </div>

            <div class="layui-input-inline ">
                <button class="layui-btn" onclick="search()">查询</button>
            </div>
        </div>

    </div>
    <table class="layui-table" id="showTable" lay-filter="showTable"></table>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="batchGen">批量生成</button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="gen">生成</a>
</script>

</body>
</html>
<!--<script src="/layui/layui.all.js"></script>-->
<!--<script src="/js/core.util.js"></script>-->
<!--<script src="/js/jquery.js"></script>-->
<!--<script src="/js/jquery.fileDownload.js"></script>-->

<script>
    //获取token
    // var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    // var tokenQuery = token.replace("#", "%23");
    var tableIns1;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;


    //判断字符是否为空的方法
    let coreUtiisEmpty = function (obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }


    layui.use(['table', 'layer', 'laydate'], function () {

        //加载table
        tableIns1 = table.render({
            elem: '#showTable'
            , contentType: 'application/json'
            // , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/sysGenerator/listByPage' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                console.log(res);
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": coreUtiisEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": coreUtiisEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'tableName', title: '表名', sort: true},
                    {field: 'tableComment', title: '表备注', sort: true},
                    {field: 'createTime', title: '表创建时间', sort: true},
                    {toolbar: "#tool", title: '操作'}
                ]
            ]
            , toolbar: '#toolbar'
        });

        //表头工具栏事件
        table.on('toolbar(showTable)', function (obj) {
            switch (obj.event) {
                case 'batchGen':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量生成的表");
                    } else {
                        var tables = [];
                        $(data).each(function (index, item) {
                            tables.push(item.tableName);
                        });
                        $.fileDownload("/sysGenerator/gen?tables=" + tables, {httpMethod: 'get'})
                    }
                    break;
            }
            ;
        });
        //操作工具栏事件
        table.on('tool(showTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'gen':
                    $.fileDownload("/sysGenerator/gen?tables=" + data.tableName, {httpMethod: 'get'})
                    break;
            }
        });

    });

    //执行查询
    function search() {
        //这里以搜索为例
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                tableName: $("#tableName").val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };

</script>